<template>
  <div class="input-container">
    <div class="input-title">
      <img src="@/assets/welcome.png" class="title_back" />
      <span class="title_text">欢迎登录</span>
    </div>
    <!-- 表单 -->
    <div class="form_wrap">
      <div class="form_label" style="margin: 32px 0 10px">登录名</div>
      <input
        class="input-name"
        type="text"
        v-model="form.userName"
        placeholder="请输入登录名"
        @blur="userNameBlur"
        :class="{ 'input-name-error': rules.userName == false }"
        :maxlength="20"
      />
      <div class="form_error-text" v-if="rules.userName == false">登录名不能为空</div>

      <div class="form_label" style="margin: 20px 0 10px">密码</div>
      <a-input-password
        class="input-name"
        v-model="form.password"
        placeholder="请输入密码"
        :max-length="50"
        @blur="passwordBlur"
        :class="{ 'input-name-error': rules.password == false }"
      />
      <div class="form_error-text" v-if="rules.password == false">密码不能为空</div>
    </div>

    <div class="over-operate">
      <div class="forgot-password" @click="handleForgot">忘记密码</div>
    </div>

    <div class="submit-operate">
      <a-spin :loading="loading">
        <button class="submit-btn" :disabled="!form.userName || !form.password" @click="handleLogin">登录</button>
      </a-spin>
    </div>
    <div class="conclusion">
      <img src="@/assets/point.png" />
      <span>建议使用谷歌（Chrome）浏览器使用体验更佳</span>
    </div>

    <div class="wx_login">
      <div class="wx_text">
        <div class="line"></div>
        <div class="text">其他登录方式</div>
        <div class="line"></div>
      </div>
      <div class="wx_logo">
        <img @click="toWxLogin" width="32" height="32" src="@/assets/wx_logo.png" />
      </div>
    </div>
    <!-- 图片验证码弹窗 -->
    <imgCodeModal ref="imgCodeModalRef" @success="handleImgCodeSuccess" @security-verification="handleSecurityVerification" />
  </div>
</template>

<script setup>
  import { ref } from 'vue'
  import { login } from '@/api/user'
  import imgCodeModal from './img-code-modal.vue'
  import { Message } from '@arco-design/web-vue'

  const emits = defineEmits(['changeIndex', 'login2GetUser'])

  const form = ref({
    userName: '',
    password: '',
    autoLogin: false, // 自动登录
  })
  const rules = ref({
    userName: true,
    password: true,
  })
  const userNameBlur = () => {
    // 验证是否有值
    if (!form.value.userName) {
      rules.value.userName = false
    } else {
      rules.value.userName = true
    }
  }
  const passwordBlur = () => {
    if (!form.value.password) {
      rules.value.password = false
    } else {
      rules.value.password = true
    }
  }

  const loading = ref(false)
  const imgCodeModalRef = ref(null)
  const handleLogin = () => {
    if (!form.value.userName || !form.value.password) {
      Message.warning('请输入用户名、密码')
      return
    }
    loading.value = true
    login({
      userName: form.value.userName,
      password: form.value.password,
    })
      .then((_res) => {
        const loginRes = _res.result
        // 登录成功
        if (loginRes.status) {
          emits('login2GetUser')
        } else {
          loading.value = false
          const statusCode = loginRes.statusCode
          if (statusCode) {
            // 需要输入验证码
            if (statusCode == 20) {
              imgCodeModalRef.value.open({ userName: form.value.userName, password: form.value.password }, 'login')
            }
            // 需要安全验证
            if (statusCode == 30) {
              // 手机号后四位
              const encryptionPhoneNumber = loginRes.message
              handleSecurityVerification(encryptionPhoneNumber)
            }
          } else {
            Message.warning('登录失败，' + loginRes.message)
          }
        }
      })
      .catch((error) => {
        Message.error(error.message)
      })
      .finally(() => {
        setTimeout(() => {
          loading.value = false
        }, 1000)
      })
  }

  // 图形验证码-登录成功
  const handleImgCodeSuccess = () => {
    emits('login2GetUser')
  }

  // 登录失败-安全验证
  const handleSecurityVerification = (encryptionPhoneNumber) => {
    emits('changeIndex', 6, {
      userName: form.value.userName,
      password: form.value.password,
      encryptionPhoneNumber: encryptionPhoneNumber,
    })
  }

  // 忘记密码
  const handleForgot = () => {
    emits('changeIndex', 2)
  }
  // 微信登录
  const toWxLogin = () => {
    emits('changeIndex', 4)
  }
</script>

<style lang="less" scoped>
  .input-container {
    width: 398px;
    margin: 0 auto;
    .input-title {
      display: flex;
      align-items: center;
      margin-left: -4px;

      .title_back {
        margin-right: 12px;
        width: 48px;
        height: 48px;
      }
      .title_text {
        font-size: 32px;
        font-weight: 600;
        color: #1e1e1e;
      }
    }
    .over-operate {
      margin-top: 16px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex-direction: row-reverse;
      .forgot-password {
        font-size: 14px;
        font-weight: 400;
        color: #165dff;
        cursor: pointer;
      }
    }
    .submit-operate {
      margin-top: 18px;
      .submit-btn {
        width: 398px;
        height: 50px;
        font-size: 16px;
        color: #ffffff;
        border-radius: 2px 2px 2px 2px;
        background: #165dff;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        box-sizing: border-box;
        text-align: center;
        font-weight: 400;
        line-height: 1.5715;
        white-space: nowrap;
        outline: none;
        cursor: pointer;
        transition: all 0.1s cubic-bezier(0, 0, 1, 1);

        &:disabled {
          background: rgba(22, 93, 255, 0.3);
        }
      }
    }
    .conclusion {
      margin-top: 24px;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 15px;
        margin-right: 3px;
      }
      font-size: 14px;
      font-weight: 400;
      color: #1e1e1e;
    }
  }

  .form_wrap {
    .form_label {
      height: 22px;
      font-size: 14px;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: #86909c;
      line-height: 22px;
    }
    .input-name {
      width: 398px;
      height: 50px;
      border-radius: 3px 3px 3px 3px;
      font-size: 14px;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      opacity: 1;
      border: 1px solid #c5c6cd;
      outline-style: none;
      padding-top: 14px;
      padding-bottom: 14px;
      padding: 0 18px;
      background: #eaf1ff;
    }
    .input-name:focus {
      outline: none;
      border: 1px solid #165dff;
    }
    .input-name-error {
      outline: none;
      border: 1px solid #ff5b5a;
    }
  }
  .form_error-text {
    height: 22px;
    font-size: 14px;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
    color: #ff5b5a;
    line-height: 22px;
    margin-top: 11px;
  }
  .wx_login {
    margin: 24px 0 10px;
    .wx_text {
      width: 100%;
      display: flex;
      align-items: center;
      .line {
        flex: 1;
        background: #d9d9d9;
        height: 1px;
      }
      .text {
        margin: 0 10px;
        font-weight: 400;
        font-size: 12px;
        color: #b1b6c0;
        line-height: 22px;
      }
    }
    .wx_logo {
      margin-top: 10px;
      text-align: center;
      img {
        cursor: pointer;
      }
    }
  }
</style>
